package com.xtoad.ecms.baseinfo.converter;

import com.xtoad.ecms.baseinfo.dto.WebPageDTO;
import com.xtoad.ecms.baseinfo.model.WebPage;
import com.xtoad.ecms.common.web.base.IConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * webPage转换器实现类
 *
 * @author xtoad
 * @date 2020/05/29
 */
@Component
public class WebPageConverter implements IConverter<WebPageDTO, WebPage> {

    @Resource
    private PageElementConverter pageElementConverter;

    /**
     * Model To DTO
     *
     * @param model 源
     * @return 目标
     */
    @Override
    public WebPageDTO toDto(WebPage model) {
        WebPageDTO dto = new WebPageDTO();
        dto.setId(model.getId());
        dto.setCreateTime(model.getCreateTime());
        dto.setCreateUser(model.getCreateUser());
        dto.setLastUpdateTime(model.getLastUpdateTime());
        dto.setLastUpdateUser(model.getLastUpdateUser());
        dto.setTitle(model.getTitle());
        dto.setUrl(model.getUrl());
        dto.setElements(pageElementConverter.toDtoList(model.getElements()));
        return dto;
    }

    /**
     * DTO To Model
     *
     * @param dto 源
     * @return 目标
     */
    @Override
    public WebPage toModel(WebPageDTO dto) {
        WebPage model = new WebPage();
        model.setId(dto.getId());
        model.setCreateTime(dto.getCreateTime());
        model.setCreateUser(dto.getCreateUser());
        model.setLastUpdateTime(dto.getLastUpdateTime());
        model.setLastUpdateUser(dto.getLastUpdateUser());
        model.setTitle(dto.getTitle());
        model.setUrl(dto.getUrl());
        model.setElements(pageElementConverter.toModelList(dto.getElements()));
        return model;
    }

    /**
     * 批量Model To DTO
     *
     * @param models 源
     * @return 目标
     */
    @Override
    public List<WebPageDTO> toDtoList(List<WebPage> models) {
        List<WebPageDTO> dtoList = new ArrayList<>();
        models.forEach(model -> dtoList.add(this.toDto(model)));
        return dtoList;
    }

    /**
     * 批量DTO To Model
     *
     * @param dtos 源
     * @return 目标
     */
    @Override
    public List<WebPage> toModelList(List<WebPageDTO> dtos) {
        List<WebPage> modelList = new ArrayList<>();
        dtos.forEach(dto -> modelList.add(this.toModel(dto)));
        return modelList;
    }
}
